package bairui.international.console.web.config;

import java.time.Duration;

import javax.servlet.MultipartConfigElement;

import org.apache.catalina.Context;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.Ssl;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.lianjing.sh.project.authority.web.config.Config_ProjectAuthority_Controller;
import com.lianjing.sh.project.config.base.profile.Project_Profile;
import com.lianjing.sh.spring.web.controller.enhance.EnableControllerEnhance;

@Configuration
@Import({
	Project_Profile.class,
	Config_WebMvcConfiguration.class,
	Config_ProjectAuthority_Controller.class
})
//@EnableAspectJAutoProxy
@EnableControllerEnhance
public class Config_Web
//	implements WebServerFactoryCustomizer<TomcatServletWebServerFactory>
{

//	@Override
//	public void customize(TomcatServletWebServerFactory factory) {
////		factory.setPort(9090);
//		factory.setPort(80);
//	    factory.getSession().setTimeout(Duration.ofHours(3));
////	    factory.setBaseDirectory(baseDirectory);
////	    factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
//	}

	@Bean
	public TomcatServletWebServerFactory servletContainer() {
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
			@Override
			protected void postProcessContext(Context context) {
				SecurityCollection collection = new SecurityCollection();
					collection.addPattern("/*");
				//
				SecurityConstraint securityConstraint = new SecurityConstraint();
					securityConstraint.setUserConstraint("CONFIDENTIAL");
					securityConstraint.addCollection(collection);
				//
				context
					.addConstraint(securityConstraint);
			}
		};
		//
	    Ssl ssl = new Ssl();
	        ssl.setKeyStore("classpath:bairui.tetong.cc.pfx");
	        ssl.setKeyStorePassword("uve5yrd5");
    	tomcat.setSsl(ssl);
    	tomcat.getSession().setTimeout(Duration.ofMinutes(30));
//		tomcat.addAdditionalTomcatConnectors(httpConnector());
		/*
		 * 配置临时缓存文件夹
		 */
//    	String location = System.getProperty("user.dir") + "/temp";
//        File tmpFile = new File(location);
//        if (!tmpFile.exists()) {
//            tmpFile.mkdirs();
//        }
//    	tomcat.setBaseDirectory(tmpFile);
    	//
		return tomcat;
	}

	/**
	 * 创建 http 连接
	 */
//    private Connector httpConnector() {
//        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//	        connector.setScheme("http");
//	        connector.setPort(8081);
//	        connector.setSecure(false);
//	        connector.setRedirectPort(1443);		// http跳转到https
//        return connector;
//    }

	/**
	 * 多文件解析配置
	 */
	@Bean
    public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
//			factory.setMaxFileSize(DataSize.ofMegabytes(50));
//	        factory.setMaxRequestSize(DataSize.ofMegabytes(200));
	        factory.setMaxFileSize("50MB");
	        factory.setMaxRequestSize("200MB");
//        /*
//         * 配置临时缓存文件夹
//         */
//        String location = System.getProperty("user.dir") + "/temp";
//        File tmpFile = new File(location);
//        if (!tmpFile.exists()) {
//            tmpFile.mkdirs();
//        }
//        factory.setLocation(location);
        //
        return factory.createMultipartConfig();
    }

}
